import requests

from api import log
from api.api_recharge import ApiRecharge
from api.api_register_login import ApiRegisterLogin
from tool import html_parser, common_assert


class TestRecharge:
    # 初始化
    def setup(self):
        # 获取session对象
        self.session = requests.session()
        # 获取ApiRecharge对象
        self.api = ApiRecharge(self.session)
        # 登录
        ApiRegisterLogin(self.session).api_login()

    # 结束
    def teardown(self):
        self.session.close()

    # 充值验证码接口测试方法
    def test01_img_code(self,random=123123):
        try:
            result = self.api.api_img_code(random)
            print(result.status_code)
            log.info("充值验证码接口执行结果为：{}".format(result.status_code))
        except Exception as e:
            log.error(e)
            raise

    # 后台充值接口测试方法
    def test02_recharge(self,expect_text="OK"):
        try:
            self.api.api_img_code(123123)
            result = self.api.api_recharge()
            log.info("后台充值接口执行结果为：{}".format(result.json()))
            # 三方充值
            data = html_parser(result)
            result = self.session.post(url=data[0], data=data[1])
            common_assert(result, expect_text=expect_text)
            log.info("三方充值接口执行结果为：{}".format(result.text))
        except Exception as e:
            log.error(e)
            raise